package com.backend_pay.dao;

import com.backend_pay.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;


@Repository //存储层bean注释，数据流层DAO
public interface UserDao extends JpaRepository<User,Integer> {
    /*
    * @Query(value="",nativequery=true)
    * public void test();*/
    /*
    id,name,createtime,lastlogintime,password
    */
    //根据名字查找用户信息(findByName)
    @Query(value="select * from user where name=?",nativeQuery=true)
    public User findByName(String name);
    //根据用户id查找用户信息(findById)
    @Query(value="select * from user where id=?",nativeQuery=true)
    public User findByUserid(Integer id);

    //原生SQL添加一个新用户
    @Modifying //update delete操作注释
    @Transactional //声明式事务管理注释 ,事务只读不需要该注释
    @Query(value="insert into user(name,password) values(?1,?2)",nativeQuery=true)
    public void savaNewUser(String name,String password);

    //删除用户
    @Modifying
    @Transactional
    @Query(value="delete from user where id=?1",nativeQuery=true)
    public void deleteUserById(Integer id);
}
